﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mapeo;
using BE;
using NHibernate;
using NHibernate.Criterion;
using System.Data;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class TipoDocumento_DAL : Controlador
    {
        public void Insertar(TipoDocumento tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        tipocp.ID = "0";
                        Sesion.Save(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }


        public void InsertarTipoCpVenta(TipoDocumentoCpVenta tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {

                        Sesion.Save(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }

        public void InsertarTipoCaja(TipoDocumentoCaja tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {

                        Sesion.Save(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }



        public void InsertarTipoCheque(TipoDocumentoCheque tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {

                        Sesion.Save(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }


        public void Eliminar(String id)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        TipoDocumento tipocp = (TipoDocumento)Sesion.CreateCriteria("TipoDocumento").Add(Restrictions.Eq("IDTipoCp", Convert.ToInt32(id))).UniqueResult();
                        Sesion.Delete(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }


        public void Actualizar(TipoDocumentoCpVenta tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        Sesion.Update(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }

        public void ActualizarTipoDocumentoCaja(TipoDocumentoCaja tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        Sesion.Update(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }


        public void ActualizarTipoDocumentoCheque(TipoDocumentoCheque tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        Sesion.Update(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }



        public TipoDocumento obtenerTipoPorId(String ID)
        {
            try
            {
                return (TipoDocumento)AyudaNHibernate.ObtenerEntidadPorID("TipoDocumento", ID);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public TipoDocumentoCpVenta obtenerTipoCpVentaPorId(String ID)
        {
            try
            {
                using (ISession Sesion = m_SessionFactory.OpenSession())
                {
                    Object Obj = (Object)Sesion.CreateCriteria("TipoDocumentoCpVenta").Add(Restrictions.Eq("IDTipoCp", Convert.ToInt32(ID))).UniqueResult();


                    return (TipoDocumentoCpVenta)Obj;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public TipoDocumentoCaja obtenerTipoCajaPorId(String ID)
        {
            try
            {
                using (ISession Sesion = m_SessionFactory.OpenSession())
                {
                    Object Obj = (Object)Sesion.CreateCriteria("TipoDocumentoCaja").Add(Restrictions.Eq("IDTipoCp", Convert.ToInt32(ID))).UniqueResult();


                    return (TipoDocumentoCaja)Obj;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }




        public TipoDocumentoCheque obtenerTipoChequePorId(String ID)
        {
            try
            {
                using (ISession Sesion = m_SessionFactory.OpenSession())
                {
                    Object Obj = (Object)Sesion.CreateCriteria("TipoDocumentoCheque").Add(Restrictions.Eq("IDTipoCp", Convert.ToInt32(ID))).UniqueResult();
                    return (TipoDocumentoCheque)Obj;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        public DataTable listado(String Codigo, String Nombre,String Filtro)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    if (Filtro.Equals("CpVenta")) {
                        SqlCmd.CommandText = "sp_ListadoTipoDocumentoCpVenta";
                    }
                    else if (Filtro.Equals("Caja"))
                    {
                        SqlCmd.CommandText = "sp_ListadoTipoDocumentoCaja";

                    }

                    else if (Filtro.Equals("Cheque"))
                    {
                        SqlCmd.CommandText = "sp_ListadoTipoDocumentoCheque";

                    }


                    
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddWithValue("@in_codigo", Codigo);
                    SqlCmd.Parameters.AddWithValue("@in_nombre", Nombre);
                    SqlCmd.Parameters.AddWithValue("@in_filtro", Filtro);
                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }

    }
}
